﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DevOnServer
{
    public partial class CDataManager
    {
        public bool load()
        {
            string[] defines = new string[]
            {
                "defineItem.h",
                "defineWorld.h",
                "defineObj.h",
                "defineJob.h"
            };

            foreach (string def in defines)
            {
                if (!_loadDefine("Data/" + def))
                    return false;
            }

            if (!_loadPropItem("Data/propItem.txt")
                || !_loadPropMover("Data/propMover.txt")
                || !_loadMdlDyna("Data/mdlDyna.inc")
                || !_loadPropMoverEx("Data/propMoverEx.inc")
                || !_loadPropJob("Data/propJob.inc")
                || !_loadExpTable("Data/expTable.inc"))
                return false;

            return true;
        }

        private bool _loadDefine(string filename)
        {
            CFileReader file = new CFileReader();
            if (!file.load(filename))
            {
                CConsole.WriteLine("Can't open define file '{0}'", filename);
                return false;
            }

            string define, tok;
            while (!file.end)
            {
                if (file.getString() == "#define")
                {
                    define = file.getString();
                    tok = file.getString();

                    file.move(-1);

                    if (tok != "#define")
                        m_defines[define] = file.getInt();
                }
            }

            return true;
        }

        public void endLoading()
        {
            m_defines.Clear();
            m_defines = null;
        }

        public int getDefinedId(string id)
        {
            if (m_defines.ContainsKey(id))
                return m_defines[id];
            else
                return -1;
        }

        private Dictionary<string, int> m_defines = new Dictionary<string, int>();
    }
}